package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.GoodsActiveJoinDao;
import cn.ym.mall.entity.ActiveJoin;
import cn.ym.mall.entity.GoodsActiveJoin;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author 漆家乐
 */
public class GoodsActiveJoinDaoImpl implements GoodsActiveJoinDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 商品参加活动
     *
     * @param goodsActiveJoin
     * @return
     * @throws SQLException
     */
    @Override
    public long add(GoodsActiveJoin goodsActiveJoin) throws SQLException {
        long num = 0;
        try{
            String sql = "insert into ym_goodsActiveJoin(gid,acjstate) value(?,?);";
            num = queryRunner.update(DruidUtils.getConnection(),sql,goodsActiveJoin);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查询所有商品（活动）
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<GoodsActiveJoin> getAll() throws SQLException {
        List<GoodsActiveJoin> goodsActiveJoinList = new ArrayList<GoodsActiveJoin>();
        try{
            String sql = "select * from ym_goodsActiveJoin;";
            goodsActiveJoinList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<GoodsActiveJoin>(GoodsActiveJoin.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return goodsActiveJoinList;
    }

    /**
     * 根据id查询商品是否参加活动
     *
     * @param aid
     * @return
     * @throws SQLException
     */
    @Override
    public GoodsActiveJoin getGoodsActiveJoinByAid(Integer aid) throws SQLException {
        GoodsActiveJoin goodsActiveJoin = null;
        try{
            String sql = "select * from ym_goodsActiveJoin where aid = ?;";
            goodsActiveJoin = queryRunner.query(sql,new BeanHandler<GoodsActiveJoin>(GoodsActiveJoin.class),aid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return goodsActiveJoin;
    }

    /**
     * 分页查询商品（活动）
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<GoodsActiveJoin> getGoodsActiveJoinPage(Integer pageNo, Integer pageSize) throws SQLException {
        List<GoodsActiveJoin> goodsActiveJoinList = new ArrayList<GoodsActiveJoin>();
        try{
            String sql = "select * from ym_goodsActiveJoin order by aid desc limit ?,?;";
            goodsActiveJoinList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<GoodsActiveJoin>(GoodsActiveJoin.class),(pageNo-1)*pageSize,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return goodsActiveJoinList;
    }

    /**
     * 查询所有商品（活动）总数
     *
     * @return
     * @throws SQLException
     */
    @Override
    public long getGoodsActiveJoinsCount() throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_goodsActiveJoin;";
            count = (queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>()));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}
